﻿/**********************************************
 * 功能名称：中间体数据锁定
编写人：  杨莹
建立时间： 杨 2009年7月30日
说明：
对所有W系数，进行锁定，不允许编辑。
 * 并对个别的作业中心进行锁定。
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.PIPDeliverInfo
{
    public partial class PIPDataLock : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {


                MyDataBind();
            }
        }

        private void MyDataBind()
        {

            string MyAp = Application["accountPeriod"].ToString();

            string MyCenterName = TextBox_CenterName.Text;

            var MyCenterList = from a in db.TABC_JS_CostCenter
                               join b in (from k in db.TABC_JS_ProductInProcessDelivery
                                          group k by new {k.作业中心ID,k.会计期间ID,k.锁定} into g
                                          select new 
                                          {g.Key.作业中心ID,
                                          g.Key.会计期间ID,
                                          g.Key.锁定})
                               on a.作业中心ID equals b.作业中心ID
                               where b.会计期间ID == MyAp  && a.作业中心名称.Contains(MyCenterName)
                               orderby a.作业中心名称
                              
                               select new
                               {
                                   a.作业中心ID,
                                   a.作业中心名称,
                                   b.锁定
                               };
            if (MyCenterList.Count() > 0)
            {
                GridView_LockQuery.DataSource = MyCenterList;
                GridView_LockQuery.DataBind();
                Panel_Main.Visible = true;
            }
            else
            {

                Panel_Main.Visible = false;

            }
        
        }

   

        protected void Button_AllSelect_Click(object sender, EventArgs e)
        {


            for (int i = 0; i < GridView_LockQuery.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_LockQuery.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Lock")).Checked = true;
            
            }
        }

        protected void Button_UnSelect_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView_LockQuery.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_LockQuery.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Lock")).Checked = false;

            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string MyAp = Application["accountPeriod"].ToString();
     
            

            try 
            {
                for (int i = 0; i < GridView_LockQuery.Rows.Count; i++)
                {
                    GridViewRow myrows = GridView_LockQuery.Rows[i];

                
                        bool MyChecked = ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Lock")).Checked;
                        int MyCenterID = Convert.ToInt32(myrows.Cells[0].Text);

                        var isExistRows = from a in db.TABC_JS_ProductInProcessDelivery
                                         where a.作业中心ID == MyCenterID && a.会计期间ID == MyAp
                                         select a;

                        if (isExistRows.Count() > 0)
                        {
                            foreach(var EditRow in isExistRows)
                            {
                                //当锁定标志不是当前设置值的时候，更新数据项。
                               // if (EditRow.锁定 != MyChecked)
                               // {
                                    EditRow.锁定 = MyChecked;
                                    db.SubmitChanges();
                               // }
                            }
                          
                              
                        }

                   

                }

              


                alter = "<script>alert('数据更新成功');</script>";

                MyDataBind();
            }
            catch 
            { 
                alter = "<script>alert('数据更新失败');</script>"; 
            }


        }

 

        protected void Button_Query1_Click(object sender, EventArgs e)
        {
            MyDataBind();
        }
    }
}
